package com.workversion2.demo.controller;

import com.workversion2.demo.entity.student;
import com.workversion2.demo.repository.StudentRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Controller
public class showController {

    @Autowired
    StudentRepository studentRepository;

    private Optional<student> stu;

    @ModelAttribute(value = "zhangshang")
    public Optional<student> showStudent(){

     //这是一个修改的数据库访问测试   修改年级
        stu=studentRepository.findById(1);
        stu.get().setLevel(7);
        studentRepository.save(stu.get());
        //————————
        return  stu;
    }

    @RequestMapping(value = "/show")
    public String over(HttpServletRequest request,Model model) {
       List<String>  selected=new ArrayList<>();
       selected= Arrays.asList(request.getParameterValues("category"));
       model.addAttribute("selected",selected);
      // student zhangshang=studentMapper.getStudentByNumber(1);
        String string1=String.join("-",selected);
        stu.get().setCourse(string1);
        studentRepository.save(stu.get());
        System.out.println(stu);

      // String string1=String.join("-",selected);
        System.out.println(string1);
        return "show";
    }


}
